<script lang="ts" setup name="XtxCity">
// 选择的城市结果类型
export type CityResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
import { watch } from 'vue'
//
const falg = ref(false) //默认值false
const target = ref(null)
function toggle() {
  falg.value = !falg.value
}
//点击外部关闭弹层
onClickOutside(target, () => {
  falg.value = false
  //   getCityList()
})
// 在 *组件内部* 获取省市区数据
// 1.一次获取所有的数据(省市区)
// 2.先获取省,点击获取市,点击市获取区
// 3.用常量保存省市区数据
//泛型
type AreaList = {
  code: string
  level: number
  name: string
  areaList: AreaList[]
}
const cityList = ref<AreaList[]>([])
const cacheList = ref<AreaList[]>([])
const getCityList = async () => {
  const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  const res = await axios.get<AreaList[]>(url)
  cityList.value = res.data
  cacheList.value = res.data //在用一个数组储存一次 用于刷新是不用在调用axios发生数据
}
getCityList()
// 选择城市
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})

const areaList = (item: AreaList) => {
  if (item.level === 0) {
    changeResult.value.provinceName = item.name
    changeResult.value.provinceCode = item.code
    cityList.value = item.areaList
  }
  if (item.level === 1) {
    changeResult.value.cityName = item.name
    changeResult.value.cityCode = item.code
    cityList.value = item.areaList
  }
  if (item.level === 2) {
    changeResult.value.countyName = item.name
    changeResult.value.countyCode = item.code
    falg.value = false
    emits('changeCity', changeResult.value)
  }
}
//不用重复发生axios
watch(falg, () => {
  if (!falg.value) {
    cityList.value = cacheList.value
  }
})

defineProps<{
  userAddress: string
}>()

const emits = defineEmits<{
  (e: 'changeCity', value: CityResult): void
}>()
</script>

<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active: falg }" @click="toggle">
      <span class="value" v-if="userAddress">{{ userAddress }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 选择城市弹层 -->
    <div class="option" v-show="falg">
      <span @click="areaList(item)" class="ellipsis" v-for="item in cityList" :key="item.code">{{
        item.name
      }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
